{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "113c7c44",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "找到的电话号码有： ['010-12345678', '021-87654321']\n",
      "字符串以 Hello 开头\n"
     ]
    }
   ],
   "source": [
    "import re  # 导入正则表达式模块[2](@ref)\n",
    "\n",
    "# 假设我们有一段文本\n",
    "text = \"我的电话号码是 010-12345678，另一个是 021-87654321，欢迎联系。\"\n",
    "\n",
    "# 1. 定义一个正则表达式模式来匹配电话号码\n",
    "# 模式解释：\\d{3,4} 匹配3到4位数字（区号），- 匹配横杠，\\d{7,8} 匹配7到8位数字（号码）\n",
    "pattern = r'\\d{3}-\\d{8}'  # 这里的 r 表示原始字符串，避免转义麻烦[2](@ref)\n",
    "\n",
    "# 2. 使用 findall 方法找到所有匹配的项\n",
    "phone_numbers = re.findall(pattern, text)\n",
    "print(\"找到的电话号码有：\", phone_numbers)  # 输出：['010-12345678', '021-87654321']\n",
    "\n",
    "# 另一个例子：检查字符串是否以 \"Hello\" 开头\n",
    "if re.match(r'^Hello', \"Hello, Python!\"):\n",
    "    print(\"字符串以 Hello 开头\")\n",
    "else:\n",
    "    print(\"字符串不是以 Hello 开头\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "981e5519",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
